﻿/*
* Author: Zero
* Version 1.3
* Como el number por ahora es el ID, le seteo el number con el ID
* + uso el JavaDateParser para convertir lo que me pasa rodo en un Date
* + hago el MessageFactory.getMessages() para que me devuelva todo armado
* + no hago el VoiceMailFactory.getVoiceMails() porque todavía no está bien hecho
* + no estaba pasando el Booleano que define si la extension que se pide es de un contacto
*/

import com.dinmax.flash.central.extension.*
import com.dinmax.flash.central.extension.user.*
import com.dinmax.flash.central.extension.state.*
import com.dinmax.flash.central.extension.log.*
import com.dinmax.flash.central.extension.message.*
import com.dinmax.flash.utilities.xml.XMLUtils

class ExtensionFactory {
	
	public static function getExtensions(doc:XMLNode, contact:Boolean):Extensions {
		//trace("\n---ExtensionFactory.as | getExtensions()");
		var extensions:Extensions = new Extensions();
		
		if (doc.hasChildNodes()) {
			for (var aNode:XMLNode = doc.firstChild; aNode != null; aNode=aNode.nextSibling) {
				if (aNode.nodeType == 1) {
					var xt:Extension = ExtensionFactory.getExtension(aNode, contact);
					extensions.addItem(String(xt.getId()), xt);
				}; // fin if
			}; // fin for
		}; // fin if
		//trace("\n---ExtensionFactory.as | getExtensions()---\n");
		return extensions;
	};
	
	public static function getExtension(node:XMLNode, contact:Boolean):Extension {
		//trace("\n---\nEXTENSION FACTORY | getExtension()\n"+node+"\n---\n");
		//trace("ExtensionFactory.as | getExtension()\nXMLNode\n"+node+"\n");
		var xt:Extension = new Extension();
		xt.setId(Number(XMLUtils.getAttribute(node, "id")));
		xt.setName(XMLUtils.getAttribute(node, "name"));
		xt.setType(XMLUtils.getAttribute(node, "type"));
		xt.setNumber(xt.getId()); //Hasta que no lo arreglen uso el id
		/* 
		xt.setNumber(Number(XMLUtils.getAttribute(node, "number")));
		*/
				
		if(!contact){
			if(XMLUtils.getNode(node, "state")){
				xt.setState(StateFactory.getState(XMLUtils.getNode(node, "state")));
			};
			if(XMLUtils.getNode(node, "user")){
				xt.setUser(UserFactory.getUser(XMLUtils.getNode(node, "user")));
			};
			var log:ExtensionLog = new ExtensionLog();
			log.addItem(xt.getState().getName(), xt.getState());
			xt.setLog(log);
			//trace("ExtensionFactory.as | xt.setTextMessages()");
			if(XMLUtils.getNode(node, "messages")){
				xt.setTextMessages(TextMessageFactory.getTextMessages(XMLUtils.getNode(node, "messages")));
			};
			if(XMLUtils.getNode(node, "voicemails")){
				xt.setVoiceMessages(VoiceMessageFactory.getVoiceMessages(XMLUtils.getNode(node, "voicemails")));
			};
			
		};
				
		return xt;
	};
}; // fin de clase